/**
 * 
 */
package com.nari.heatassist.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.struts2.json.annotations.JSON;

import com.nari.baseinfo.model.IBuilding;
import com.nari.baseinfo.model.ICommunity;
import com.nari.baseinfo.model.IProject;
import com.nari.baseinfo.service.IBuildingService;
import com.nari.baseinfo.service.ICommunityService;
import com.nari.baseinfo.service.IProjectService;
import com.nari.coreframe.action.struts2.JSONActionSupport;
import com.nari.heatassist.service.IBuildingConsumeStatisService;
import com.nari.heatassist.utils.ConsumeStatisReportUtil;
import com.nari.heatassist.utils.ReportBuildupData;
import com.nari.platform.date.CalendarUtils;

/**
 * 楼栋耗热统计分析
 * @author Xiaopeng
 * @date 2014-4-25
 */
public class BuildingConsumeStatisAction extends JSONActionSupport {
	private static final long serialVersionUID = -4557439872146188064L;
	private IProjectService iProjectService;
	private ICommunityService iCommunityService;
	private IBuildingService iBuildingService;
	private IBuildingConsumeStatisService iBuildingConsumeStatisService;
	//统计条件
	private List<IProject> projectList;
	private List<ICommunity> communityList;
	private List<IBuilding> buildingList;
	//统计参数
	private String projectUID;
	private String communityUID;
	private String buildingUID;
	private Date startDate;
	private Date endDate;
	private String timeFrequence; //统计频率：permonth-每月	peryear-每年
	private String chartType; //报表类型：line-线性图	column-柱状图
	//报表数
	private ICommunity community;
	private IBuilding building;
	private String chartXmlData;
	private String measureReportInfo;
	
	/**
	 * 初始化楼栋耗热分析条件
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-10-9
	 */
	public String initBuildingConsumeCond() throws Exception {
		//所属项目列表
		this.projectList = this.iProjectService.queryAllProjectInOrder();
		if(this.projectList == null) {
			this.projectList = new ArrayList<IProject>();
		}
		//所属小区列表
		if(this.projectUID!=null && !"".equals(projectUID)) {
			this.communityList = this.iCommunityService.queryCommunitysByBelongings(null, null, null, projectUID);
		} else {
			this.communityList = new ArrayList<ICommunity>();
		}
		//所属楼栋列表
		this.buildingList = this.iBuildingService.queryBuildingByBelongings(projectUID, communityUID);
		return "initConsumeCond";
	}
	
	/**
	 * 小区范围所属楼栋的耗热报表分析
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-4-25
	 */
	public String queryCommunityAreaStatisChart() throws Exception {
		//默认情况下生成无结构数据的报表XML
		this.chartXmlData = ConsumeStatisReportUtil.genReportChartXml("", "", "楼栋", "单位耗热量(单位：MWh/m&sup2;)", "");
		//查询小区信息
		if(communityUID!=null && !"".equals(communityUID)) {
			this.community = iCommunityService.queryEntityById(communityUID);
		} 
		//查询楼栋信息
		if(community != null){
			this.buildingList = iBuildingService.queryBuildingByBelongings(null, communityUID);
		} 
		//根据楼栋信息统计匹配时段的耗热量数据
		if(buildingList!=null && buildingList.size()>0) {
			ReportBuildupData reportBuildupData = iBuildingConsumeStatisService.queryBuildingConsumeReport(buildingList, startDate, endDate);
			//报告异常配置
			if(!reportBuildupData.getRmsg().isSuccess()) {
				this.measureReportInfo = reportBuildupData.getRmsg().getMsg();
			}
			//报表配置信息
			String caption = "";
			if(community.getCommunityName().endsWith("小区")) {
				caption = community.getCommunityName() + "所有楼栋耗热量对比分析";
			} else {
				caption = community.getCommunityName() + "小区所有楼栋耗热量对比分析";
			}
			String subcaption = "from "+CalendarUtils.parseDateToString(startDate)
					+" to "+CalendarUtils.parseDateToString(endDate);
			//报表数据
			this.chartXmlData = ConsumeStatisReportUtil.genReportChartXml(caption, subcaption, "楼栋", "单位耗热量(单位：MWh/m&sup2;)", reportBuildupData.getReportData());
		}
		return "communityChart";
	}
	
	/**
	 * 单个楼栋的耗热报表分析
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-4-25
	 */
	public String queryBuildingAreaStatisChart() throws Exception {
		//默认情况下生成无结构数据的报表XML
		this.chartXmlData = ConsumeStatisReportUtil.genReportChartXml("", "", "楼栋", "单位耗热量(单位：MWh/m&sup2;)", "");
		//查询楼栋信息
		if(buildingUID!=null && !"".equals(buildingUID)) {
			this.building = iBuildingService.queryEntityById(buildingUID);
		}
		//根据楼栋信息和统计频率统计匹配时段的耗热量数据
		if(building != null) {
			ReportBuildupData reportBuildupData = iBuildingConsumeStatisService.queryBuildingConsumeReport(building, startDate, endDate, timeFrequence);
			//报告异常配置
			if(!reportBuildupData.getRmsg().isSuccess()) {
				this.measureReportInfo = reportBuildupData.getRmsg().getMsg();
			}
			//报表配置信息
			String caption = "";
			if(building.getBuildingID().endsWith("楼")) {
				caption = building.getBuildingID() + "耗热量";
			} else {
				caption = building.getBuildingID() + "楼耗热量";
			}
			if("peryear".equals(timeFrequence)) {
				caption += "年度统计分析";
			}
			if("permonth".equals(timeFrequence)) {
				caption += "月度统计分析";
			}
			String subcaption = "from "+CalendarUtils.parseDateToString(startDate)
					+" to "+CalendarUtils.parseDateToString(endDate);
			//报表数据
			this.chartXmlData = ConsumeStatisReportUtil.genReportChartXml(caption, subcaption, "时段", "单位耗热量(单位：MWh/m&sup2;)", reportBuildupData.getReportData());
		}
		return "buildingChart";
	}

	/**
	 * @return the iProjectService
	 */
	@JSON(serialize=false)
	public IProjectService getiProjectService() {
		return iProjectService;
	}

	/**
	 * @param iProjectService the iProjectService to set
	 */
	public void setiProjectService(IProjectService iProjectService) {
		this.iProjectService = iProjectService;
	}

	/**
	 * @return the iCommunityService
	 */
	@JSON(serialize=false)
	public ICommunityService getiCommunityService() {
		return iCommunityService;
	}

	/**
	 * @param iCommunityService the iCommunityService to set
	 */
	public void setiCommunityService(ICommunityService iCommunityService) {
		this.iCommunityService = iCommunityService;
	}

	/**
	 * @return the iBuildingService
	 */
	@JSON(serialize=false)
	public IBuildingService getiBuildingService() {
		return iBuildingService;
	}

	/**
	 * @param iBuildingService the iBuildingService to set
	 */
	public void setiBuildingService(IBuildingService iBuildingService) {
		this.iBuildingService = iBuildingService;
	}

	/**
	 * @return the iBuildingConsumeStatisService
	 */
	@JSON(serialize=false)
	public IBuildingConsumeStatisService getiBuildingConsumeStatisService() {
		return iBuildingConsumeStatisService;
	}

	/**
	 * @param iBuildingConsumeStatisService the iBuildingConsumeStatisService to set
	 */
	public void setiBuildingConsumeStatisService(
			IBuildingConsumeStatisService iBuildingConsumeStatisService) {
		this.iBuildingConsumeStatisService = iBuildingConsumeStatisService;
	}

	/**
	 * @return the projectList
	 */
	public List<IProject> getProjectList() {
		return projectList;
	}

	/**
	 * @param projectList the projectList to set
	 */
	public void setProjectList(List<IProject> projectList) {
		this.projectList = projectList;
	}

	/**
	 * @return the communityList
	 */
	public List<ICommunity> getCommunityList() {
		return communityList;
	}

	/**
	 * @param communityList the communityList to set
	 */
	public void setCommunityList(List<ICommunity> communityList) {
		this.communityList = communityList;
	}

	/**
	 * @return the buildingList
	 */
	public List<IBuilding> getBuildingList() {
		return buildingList;
	}

	/**
	 * @param buildingList the buildingList to set
	 */
	public void setBuildingList(List<IBuilding> buildingList) {
		this.buildingList = buildingList;
	}

	/**
	 * @return the projectUID
	 */
	public String getProjectUID() {
		return projectUID;
	}

	/**
	 * @param projectUID the projectUID to set
	 */
	public void setProjectUID(String projectUID) {
		this.projectUID = projectUID;
	}

	/**
	 * @return the communityUID
	 */
	public String getCommunityUID() {
		return communityUID;
	}

	/**
	 * @param communityUID the communityUID to set
	 */
	public void setCommunityUID(String communityUID) {
		this.communityUID = communityUID;
	}

	/**
	 * @return the buildingUID
	 */
	public String getBuildingUID() {
		return buildingUID;
	}

	/**
	 * @param buildingUID the buildingUID to set
	 */
	public void setBuildingUID(String buildingUID) {
		this.buildingUID = buildingUID;
	}

	/**
	 * @return the startDate
	 */
	public Date getStartDate() {
		return startDate;
	}

	/**
	 * @param startDate the startDate to set
	 */
	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	/**
	 * @return the endDate
	 */
	public Date getEndDate() {
		return endDate;
	}

	/**
	 * @param endDate the endDate to set
	 */
	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	/**
	 * @return the timeFrequence
	 */
	public String getTimeFrequence() {
		return timeFrequence;
	}

	/**
	 * @param timeFrequence the timeFrequence to set
	 */
	public void setTimeFrequence(String timeFrequence) {
		this.timeFrequence = timeFrequence;
	}

	/**
	 * @return the chartType
	 */
	public String getChartType() {
		return chartType;
	}

	/**
	 * @param chartType the chartType to set
	 */
	public void setChartType(String chartType) {
		this.chartType = chartType;
	}

	/**
	 * @return the community
	 */
	public ICommunity getCommunity() {
		return community;
	}

	/**
	 * @param community the community to set
	 */
	public void setCommunity(ICommunity community) {
		this.community = community;
	}

	/**
	 * @return the building
	 */
	public IBuilding getBuilding() {
		return building;
	}

	/**
	 * @param building the building to set
	 */
	public void setBuilding(IBuilding building) {
		this.building = building;
	}

	/**
	 * @return the chartXmlData
	 */
	public String getChartXmlData() {
		return chartXmlData;
	}

	/**
	 * @param chartXmlData the chartXmlData to set
	 */
	public void setChartXmlData(String chartXmlData) {
		this.chartXmlData = chartXmlData;
	}

	/**
	 * @return the measureReportInfo
	 */
	public String getMeasureReportInfo() {
		return measureReportInfo;
	}

	/**
	 * @param measureReportInfo the measureReportInfo to set
	 */
	public void setMeasureReportInfo(String measureReportInfo) {
		this.measureReportInfo = measureReportInfo;
	}
	
}
